//
//  HWTools.m
//  OCDome
//
//  Created by 李含文 on 2020/7/30.
//  Copyright © 2020 李含文. All rights reserved.
//

#import "HWTools.h"

UIWindow *HW_MainWindow(void) {
    UIWindow *window = nil;
    if (@available(iOS 13.0, *)) {
        window = [UIApplication sharedApplication].windows.firstObject;
    } else {
        window = [UIApplication sharedApplication].delegate.window;
    }
    return window;
}

/// 获取当前可视控制器
UIViewController *hw_getVisibleViewController(void) {
    UIViewController *currentViewController = HW_MainWindow().rootViewController;
    while (1) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        }else {
            if ([currentViewController isKindOfClass:[UINavigationController class]]) {
                currentViewController = ((UINavigationController *)currentViewController).visibleViewController;
            }else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
                currentViewController = ((UITabBarController* )currentViewController).selectedViewController;
            }else {
                break;
            }
        }
    }
    return currentViewController;
}
@implementation HWTools

+ (CGFloat)statusBarHight {
    float statusBarHeight = 20;
    if (@available(iOS 13.0, *)) {
        UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
        statusBarHeight = statusBarManager.statusBarFrame.size.height;
    } else {
        statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    }
    return statusBarHeight;
}

+ (BOOL)isIphoneX {
    return [self statusBarHight]>20?YES:NO;
}

+ (CGFloat)safeHeight {
    return [self isIphoneX]?34:0;
}

+ (CGFloat)screenHeight {
    return [UIScreen mainScreen].bounds.size.height;
}

+ (CGFloat)screenWidth {
    return [UIScreen mainScreen].bounds.size.width;
}

+ (CGFloat)tabBarHeight {
    return [self isIphoneX]?83:49;
}

+ (CGFloat)navHeight {
    return [self statusBarHight]+44;
}
@end
